package me.linkcn.ydtxl.server.resources;

import me.linkcn.ydtxl.server.objects.Group;
import me.linkcn.ydtxl.server.objects.ObjectsFacade;
import org.codehaus.jackson.map.ObjectMapper;
import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ResourceException;

import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 * User: cmcc
 * Date: 11-8-15
 * Time: 上午12:37
 */
public class GroupResource extends BaseResource {
    String accountName;
    String groupName;

    @Override
    protected void doInit() throws ResourceException {
        this.accountName = (String) getRequest().getAttributes().get("accountName");
        this.groupName = (String) getRequest().getAttributes().get("groupName");
    }

    @Get("json")
    public Representation getGroupDetail() {
        allowOtherHost();
        Group group = null;
        if (this.groupName != null && this.accountName != null) {
            ObjectsFacade dataFacade = getObjectsFacade();
            group = dataFacade.getGroupByAccountNameAndGroupName(this.accountName, this.groupName);
        }
        if (group != null) {
            String result = null;
            try {
                ObjectMapper mapper = new ObjectMapper();
                result = mapper.writeValueAsString(group);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new StringRepresentation(result);
        } else {
            return generateErrorRepresentation("通讯录组不存在！", "1");
        }
    }

    @Post
    public Representation acceptGroup(Representation input) {
        allowOtherHost();
        System.out.println(input.getMediaType());
        JacksonRepresentation<Group> groupRep = new JacksonRepresentation<Group>(input, Group.class);
        Group group = groupRep.getObject();
        if (this.accountName != null) {
            group.setAccountName(this.accountName);
        } else {
            return null;
        }
        ObjectsFacade dataFacade = getObjectsFacade();
        int _id = dataFacade.generateGroupIdByAccountName(group.getAccountName());
        if (_id > -1) {
            _id = _id + 1;
            group.set_id(_id);
        } else {
            return null;
        }
        System.out.println("_id:" + group.get_id());
        System.out.println("accountType:" + group.getAccountType());
        System.out.println("accountName:" + group.getAccountName());
        System.out.println("title:" + group.getTitle());
        System.out.println(getRequest().getResourceRef());
        boolean result = false;
        if (group.getAccountName() != null && group.getTitle() != null && group.get_id() > -1) {
            String gid = UUID.randomUUID().toString().replaceAll("-", "");
            group.setGid(gid);
            group.setAccountType("com.139");
            group.setPackageId(0);
            group.setSourceid("");
            group.setVersion(2);
            group.setDirty(1);
            group.setTitleRes("");
            group.setNotes("test note " + group.getTitle());
            group.setSystemId("test");
            group.setDeleted(0);
            group.setGroupVisible(1);
            group.setShouldSync(1);
            group.setSync1("");
            group.setSync2("");
            group.setSync3("");
            group.setSync4("");
            result = dataFacade.createGroup(group);
        }
        if (result) {
            return new JacksonRepresentation<Group>(group);
        } else {
            return generateErrorRepresentation("创建通讯录组失败！", "2");
        }
    }

    @Delete
    public Representation deleteGroup() {
        allowOtherHost();
        boolean result = false;
        if (this.groupName != null && this.accountName != null) {
            ObjectsFacade dataFacade = getObjectsFacade();
            result = dataFacade.deleteGroupByAccountNameAndGroupName(this.accountName, this.groupName);
        }
        return generateResultRepresentation(result);
    }
}